package com.jeecg.p3.dzdp.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeecg.p3.dzdp.dao.WxActDzdpPropertyDao;
import com.jeecg.p3.dzdp.dao.WxActDzdpRelationDao;
import com.jeecg.p3.dzdp.entity.WxActDzdpProperty;
import com.jeecg.p3.dzdp.entity.WxActDzdpRelation;
import com.jeecg.p3.dzdp.service.WxActDzdpPropertyService;

@Service("wxActDzdpPropertyService")
public class WxActDzdpPropertyServiceImpl implements WxActDzdpPropertyService {
	@Resource
	private WxActDzdpPropertyDao wxActDzdpPropertyDao;
	@Autowired
	private WxActDzdpRelationDao wxActDzdpRelationDao;

	@Override
	public void doAdd(WxActDzdpProperty wxActDzdpProperty) {
		wxActDzdpPropertyDao.add(wxActDzdpProperty);
		List<WxActDzdpRelation> optsList=wxActDzdpProperty.getOptsList();
		if(optsList!=null){
		for(WxActDzdpRelation relation:optsList){
			relation.setProperty_Id(wxActDzdpProperty.getId());
		}
		wxActDzdpRelationDao.batchInsert(optsList);
		}
	}

	@Override
	public void doEdit(WxActDzdpProperty wxActDzdpProperty) {
 		wxActDzdpPropertyDao.update(wxActDzdpProperty);
		List<WxActDzdpRelation> optsList=wxActDzdpProperty.getOptsList();
		List<String> ids=new ArrayList<String>();
		if(optsList!=null){
			for(WxActDzdpRelation relation:optsList){
				if(StringUtils.isNotEmpty(relation.getId())){
				ids.add(relation.getId());
				}
			}
			wxActDzdpRelationDao.batchDeleteOldOpts(ids, wxActDzdpProperty.getId());//删除不存在的建议项
			for(WxActDzdpRelation relation:optsList){
				if(StringUtils.isEmpty(relation.getId())){
					relation.setProperty_Id(wxActDzdpProperty.getId());
					wxActDzdpRelationDao.add(relation);
				}else{
					wxActDzdpRelationDao.update(relation);
				}
			}
		}else{
			wxActDzdpRelationDao.batchDeleteOldOpts(ids, wxActDzdpProperty.getId());//删除不存在的建议项
		}
	}

	@Override
	public void doDelete(String id) {
		wxActDzdpPropertyDao.delete(id);
	}

	@Override
	public WxActDzdpProperty queryById(String id) {
		WxActDzdpProperty wxActDzdpProperty  = wxActDzdpPropertyDao.get(id);
		return wxActDzdpProperty;
	}

	@Override
	public PageList<WxActDzdpProperty> queryPageList(
		PageQuery<WxActDzdpProperty> pageQuery) {
		PageList<WxActDzdpProperty> result = new PageList<WxActDzdpProperty>();
		Integer itemCount = wxActDzdpPropertyDao.count(pageQuery);
		List<WxActDzdpProperty> list = wxActDzdpPropertyDao.queryPageList(pageQuery,itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}
	
	@Override
	public List<WxActDzdpProperty> queryPropertyByBaseId(String BaseId){
		return wxActDzdpPropertyDao.queryPropertyByBaseId(BaseId);
	}
	@Override
	public List<WxActDzdpProperty> queryPropertyInIds(String ids){
		return wxActDzdpPropertyDao.queryPropertyInIds(ids);
	}
	
}
